package top.usts.springframework.beans.factory.support;

import top.usts.springframework.BeanFactory;
import top.usts.springframework.BeansException;
import top.usts.springframework.beans.factory.config.BeanDefinition;

/**
 * @description:
 * @author: CJ
 * @date: 2022/2/21
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) throws BeansException {
        Object bean = getSingleton(name);
        if (bean != null) {
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition);
    }


    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;


    protected abstract Object createBean(String name, BeanDefinition beanDefinition) throws BeansException;
}
